package org.jeecg.modules.demo.zhidingyueduzhongdiangongzuo.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.yuedushenhe.entity.Yuedushenhefankuibiao;
import org.jeecg.modules.demo.yuedushenhe.mapper.YuedushenhefankuibiaoMapper;
import org.jeecg.modules.demo.zhidingyueduzhongdiangongzuo.entity.Zhidingyueduzhongdiangongzuo;
import org.jeecg.modules.demo.zhidingyueduzhongdiangongzuo.entity.Yuedupeihebumen;
import org.jeecg.modules.demo.zhidingyueduzhongdiangongzuo.mapper.YuedupeihebumenMapper;
import org.jeecg.modules.demo.zhidingyueduzhongdiangongzuo.mapper.ZhidingyueduzhongdiangongzuoMapper;
import org.jeecg.modules.demo.zhidingyueduzhongdiangongzuo.service.IZhidingyueduzhongdiangongzuoService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 制定月度重点工作
 * @Author: jeecg-boot
 * @Date:   2025-11-07
 * @Version: V1.0
 */
@Service
public class ZhidingyueduzhongdiangongzuoServiceImpl extends ServiceImpl<ZhidingyueduzhongdiangongzuoMapper, Zhidingyueduzhongdiangongzuo> implements IZhidingyueduzhongdiangongzuoService {

	@Autowired
	private ZhidingyueduzhongdiangongzuoMapper zhidingyueduzhongdiangongzuoMapper;
	@Autowired
	private YuedupeihebumenMapper yuedupeihebumenMapper;
	@Autowired
	private YuedushenhefankuibiaoMapper yuedushenhefankuibiaoMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Zhidingyueduzhongdiangongzuo zhidingyueduzhongdiangongzuo, List<Yuedupeihebumen> yuedupeihebumenList) {
		zhidingyueduzhongdiangongzuoMapper.insert(zhidingyueduzhongdiangongzuo);
		if(yuedupeihebumenList!=null && yuedupeihebumenList.size()>0) {
			for(Yuedupeihebumen entity:yuedupeihebumenList) {
				//外键设置
				entity.setZhidingid(zhidingyueduzhongdiangongzuo.getId());
				entity.setZhuangtai("部长待接收");
				yuedupeihebumenMapper.insert(entity);
				Yuedushenhefankuibiao yuedushenhefankuibiao = new Yuedushenhefankuibiao();
				yuedushenhefankuibiao.setShenheid(entity.getId());
				System.out.println("审核id："+entity.getId());
				yuedushenhefankuibiao.setZhidingid(zhidingyueduzhongdiangongzuo.getId());
				yuedushenhefankuibiao.setZerenzhuguan(zhidingyueduzhongdiangongzuo.getZerenzhuguan());
				yuedushenhefankuibiao.setPeihebumen(entity.getPeihebumen());
				yuedushenhefankuibiao.setZhuangtai("进行中");
				yuedushenhefankuibiao.setWanchengshijian(entity.getWanchengshijian());
				yuedushenhefankuibiaoMapper.insert(yuedushenhefankuibiao);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Zhidingyueduzhongdiangongzuo zhidingyueduzhongdiangongzuo,List<Yuedupeihebumen> yuedupeihebumenList) {
		zhidingyueduzhongdiangongzuoMapper.updateById(zhidingyueduzhongdiangongzuo);
		
		//1.先删除子表数据
		yuedupeihebumenMapper.deleteByMainId(zhidingyueduzhongdiangongzuo.getId());
		
		//2.子表数据重新插入
		if(yuedupeihebumenList!=null && yuedupeihebumenList.size()>0) {
			for(Yuedupeihebumen entity:yuedupeihebumenList) {
				//外键设置
				entity.setZhidingid(zhidingyueduzhongdiangongzuo.getId());
				yuedupeihebumenMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		yuedupeihebumenMapper.deleteByMainId(id);
		zhidingyueduzhongdiangongzuoMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			yuedupeihebumenMapper.deleteByMainId(id.toString());
			zhidingyueduzhongdiangongzuoMapper.deleteById(id);
		}
	}

	//获取部门id
	public String getDepartId() {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departId = sysUser.getDepartIds();
		return departId;
	}
	//获取责任主管
	@Override
	public List<SysUser> getZerenzhuguanid() {
		//获取部门id
		String departId = getDepartId();
		return zhidingyueduzhongdiangongzuoMapper.queryUserList(departId);
	}

	@Override
	public List<SysDepart> getPeihebumen(){
		return zhidingyueduzhongdiangongzuoMapper.queryDepartList();
	}
}
